﻿var $scat;
var $sitem;
var cArray;
var $curCat = null;
var $curSkillOption = null;
var $curCatOption = null;
var $xml = null;

$(function () {
    $scat = $('select#scat');
    $sitem = $('select#sitem');
    $scat.change(GetSkillByCat);
    $sitem.change(function () {
        $curSkillOption = $(this).find("option:selected");
    });
    $('#delitembutton').click(DelItem);
    $('#additembutton').click(AddItem);
    $('#addcatbutton').click(AddCat);
    $('#btnSubmit').click(DoSubmit);
    $('#delcatbutton').click(DelCat);
    $('#renameitembutton').click(RenameItem);
    $('#renamecatbutton').click(RenameCat);

    ReadXml();
    ParseXmlAndFillCategory();
});

function RenameCat() {
    var curName = $scat.val();
    if (curName == null) {
        return false;
    }
    else {
        
        var newName = prompt("新名称：", curName);
        if (null != newName && newName.length > 0) {
            var $cat = $xml.find("Category[Name='" + newName + "']");
            if ($cat.length > 0) {
                CallOM(newName + " 已经存在");
                return false;
            }
            else {
                $curCatOption.text(newName).val(newName);
                $curCat.attr("Name", newName);
            }
        }
    }
}

function RenameItem() {
    var curName = $sitem.val();
    if (curName == null) {
        return false;
    }
    else {
        var newName = prompt("新名称：", curName);
        if (null != newName && newName.length > 0) {
            var $item = $curCat.find("Item[Name='" + newName + "']");
            if ($item.length > 0) {
                CallOM(itemName + " 已经存在");
                return false;
            }
            else {
                $curSkillOption.text(newName).val(newName);
                $curCat.find("Item[Name='" + curName + "']").attr("Name", newName);
            }
        }
    }
}

function ParseXmlAndFillCategory() {
    $xml.find("Category").each(function (k, v) {
        var cName = $(v).attr("Name");
        var $option = $("<option value='" + cName + "'>" + cName + "</option>");
        $scat.append($option);
    });
}

function DoSubmit() {
    ConvertJson();
    this.form.submit();
}

function DelCat() {
    if ($curCatOption == null) {
        CallOM("请选择大类。");
        return;
    }
    else if (confirm("确定要删除 " + $curCatOption.val() + " 吗？所有的子类都将被删除。")) {
        $scat.find("option[value='" + $curCatOption.val() + "']").remove();
        $curCat.remove();
        $sitem.empty();
        $curCat = null;
        $curCatOption = null;
    }

}

function GetSkillByCat() {
    $curCatOption = $(this).find("option:selected");
    $curCat = $xml.find("Category[Name='" + $curCatOption.val() + "']");
    $sitem.empty();
    $curCat.children().each(function (k, v) {
        var sName = $(v).attr("Name");
        var $option = $("<option value='" + sName + "'>" + sName + "</option>");
        $sitem.append($option);
    });
}

function DelItem() {
    if (null != $sitem.val()) {
        var itemVal = $sitem.val();
        $sitem.find("option[value='" + itemVal + "']").remove();
        $curCat.find("Item[Name='" + itemVal + "']").remove();
    }
}

function AddItem() {
    if (null == $curCatOption) {
        CallOM("请选择大类。");
        return false;
    }
    var shouldAdd = true;
    var itemName = prompt("为“" + $curCatOption.val() + "”增加一个子类", "");
    if (null == itemName) return false;

    var $item = $curCat.find("Item[Name='" + itemName + "']");
    if ($item.length > 0) {
        CallOM(itemName + " 已经存在");
        return false;
    }
    else {
        var $option = $("<option value='" + itemName + "'>" + itemName + "</option>");
        $sitem.append($option);
        $curCat.appendXml('<Item Name="' + itemName + '" Guid="' + NewGuid() + '" />');
    }
}

function AddCat() {
    var shouldAdd = true;
    var itemName = prompt("增加一个大类", "");
    if (null == itemName || itemName.length == 0) return false;

    var $tCat = $xml.find("Category[Name='" + itemName + "']");
    if ($tCat.length > 0) {
        CallOM(itemName + "已经存在。");
    }
    else {
        var $option = $("<option value='" + itemName + "'>" + itemName + "</option>");
        $scat.append($option);
        $xml.children().first().appendXml("<Category Guid=\"" + NewGuid() + "\" Name=\"" + itemName + "\" />");
        $scat.children().last().attr('selected', 'selected');
        $curCatOption = $scat.children().last();
        $sitem.empty();
        $curCat = $xml.find("Category[Name='" + itemName + "']");
    }
}

function ConvertJson() {
    $('#hdnCatList').val($xml.xml());
}

function ReadXml() {
    var xml = "";
    $.ajax({
        async: false,
        type: "GET",
        url: relRootPath + "/Services/GetLearningSkillXml.ashx?t=" + new Date(),
        //dataType: "xml",
        success: function (x, h, r) {
            $xml = $(x);
        }
    });

    return xml;
}

